package com.wyp168.api;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;

public class API_ {
    public static void main(String[] args) throws UnknownHostException {

        //获取本机的InetAddress 对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println("localhost:" + localHost);   ////LAPTOP-P65PFGKK/10.30.4.228

        //根据指定主机名，获取 InetAddress 对象
        InetAddress byName = InetAddress.getByName("LAPTOP-P65PFGKK");
        System.out.println("byName: " + byName); //LAPTOP-P65PFGKK/10.30.4.228

        //根据域名返回 InetAddress对象，比如:https://www.baidu.com
        InetAddress allByName = InetAddress.getByName("www.baidu.com");
        System.out.println("allByName: " + allByName);  //www.baidu.com/180.101.49.14

        //根据InetAddress 对象 获取对应的IP地址
        String address = allByName.getHostAddress();
        System.out.println(address);

        //通过InetAddress 对象， 获取对应的主机名/或者是域名
        String hostName = allByName.getHostName();
        System.out.println("host 对应的主机名/域名: " + hostName);
    }
}
